
<template>
  <el-dialog v-model="ifShow" :title="modelTitle" width="1000" :before-close="handleCancel">
    <Amap ref="map" :rowMapLocData="rowMapLocData" />
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item label="城市" prop="cityId">
        <el-select filterable clearable v-model="formData.cityId" placeholder="请选择" @change="handleCityChange">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择" size="large">
          <el-option :value="1" label="机场"></el-option>
          <el-option :value="2" label="火车站"></el-option>
          <el-option :value="3" label="城市"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="站点ID" prop="stationId">
        <el-input v-model="formData.stationId" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="站点名称" prop="stationName">
        <el-input v-model="formData.stationName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="经纬度" prop="point">
        <el-input v-model="formData.point" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import JntApi from "@/api/jnt";
import Amap from "./map.vue";
import { channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";

const props = defineProps<{}>();
const emit = defineEmits(["success"]);
const formRef: any = ref();
let formData = ref({
  id: "",
  type: null,
  stationId: null,
  stationName: null,
  cityId: null,
  cityName: null,
  points: null,
  point: null,
});
const rules = reactive({
  type: [{ required: true, message: "请选择" }],
  stationId: [{ required: true, message: "请输入" }],
  stationName: [{ required: true, message: "请输入" }],
  cityId: [{ required: true, message: "请选择" }],
  point: [{ required: true, message: "请选择" }],
});
const ifShow = ref(false);
const modelTitle = ref();
const cityOptions = ref();
const rowMapLocData = ref(); //回显围栏所需数据、
const map = ref();
function edit(title: any, row: any) {
  modelTitle.value = title;
  ifShow.value = true;
  //清空围栏数据
  rowMapLocData.value = [];
  //编辑
  if (title == "编辑") {
    rowMapLocData.value = row.points.map((str: any) => str.split(",").map(Number));
  }

  if (title == "新增") {
    formData.value = {
      id: "",
      type: null,
      stationId: null,
      stationName: null,
      cityId: null,
      cityName: null,
      points: null,
      point: null,
    };
  }
  Object.assign(formData.value, row);
}

/**
 * 确定按钮点击事件
 */
async function handleOk() {
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      let arr = await map.value.getLoction();
      formData.value.points = arr.map((subArray) => subArray.join(","));
      let model = formData.value;
      JntApi.stationEdit(model).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          formRef.value.resetFields();
          emit("success");
          handleCancel();
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}

//城市下拉框点击事件
function handleCityChange(value: any) {
  formData.value.cityName = cityOptions.value.find((item: any) => item.id == value).name;
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  formRef.value.resetFields();
  ifShow.value = false;
}

defineExpose({
  edit,
});
onMounted(() => {
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.flex_box {
  display: flex;
  margin-bottom: 14px;
  position: relative;
  .add_btn {
    position: absolute;
    right: -80px;
  }
}
</style>
